{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'QuantumCircuit' from 'qiskit.circuit' (unknown location)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcircuit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QuantumCircuit, QuantumRegister, ClassicalRegister\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m transpile\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit_aer\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Aer\n",
      "\u001b[0;31mImportError\u001b[0m: cannot import name 'QuantumCircuit' from 'qiskit.circuit' (unknown location)"
     ]
    }
   ],
   "source": [
    "from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister\n",
    "from qiskit import transpile\n",
    "from qiskit_aer import Aer\n",
    "from qiskit import execute\n",
    "import random\n",
    "import numpy as np\n",
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "from qmg.utils import MoleculeQuantumStateGenerator, CircuitBuilder\n",
    "from typing import List, Union\n",
    "from rdkit import RDLogger\n",
    "RDLogger.DisableLog('rdApp.*')\n",
    "\n",
    "# from cusvaer.backends import StatevectorSimulator\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29\n",
      "[0.2307445 0.7692555]\n",
      "[0.14489081 0.51074485 0.41878899 0.77822716]\n",
      "[0.91201822 0.02265492 0.06532686]\n",
      "[0.3607422  0.85559588 0.46822029 0.7110535  0.41501785 0.83515278]\n",
      "{'100000 01 1000 10 10 1010': 1, '000010 11 1000 10 10 1010': 1, '100000 11 1000 11 11 1010': 1, '000010 10 0110 10 11 1010': 1, '000010 01 1010 11 10 1010': 1, '000000 00 1000 11 11 1111': 1, '000010 11 0100 10 11 1010': 1, '000010 10 1100 01 11 1010': 1, '000000 00 1100 11 10 1010': 1, '100000 01 1000 11 10 1010': 1, '000000 00 0100 11 10 1111': 1, '000000 00 0000 00 11 1110': 1, '000010 11 0100 10 01 1111': 1, '000010 11 1000 01 01 1010': 2, '000010 01 1000 11 10 1010': 2, '000000 00 0000 00 11 1111': 1, '000010 11 1100 11 11 1010': 2, '000010 11 1001 11 10 1010': 1, '000001 11 0100 11 11 1010': 3, '000001 11 1100 11 10 1010': 1, '000001 11 1000 11 01 1010': 2, '000010 11 0010 10 10 1010': 1, '000010 01 1100 11 11 1010': 1, '000000 00 1000 11 11 1011': 1, '000010 01 0100 11 11 1010': 1, '000000 00 0110 01 11 1010': 1, '000000 00 1100 11 11 1010': 2, '000000 00 1010 11 10 1010': 1, '000001 11 1000 11 10 1011': 2, '000010 11 1000 01 10 1010': 1, '000001 11 0100 10 11 1010': 2, '000001 11 0100 11 10 1010': 3, '000010 11 1000 11 10 1011': 1, '000010 11 0100 11 10 1010': 7, '000001 01 0100 01 01 1010': 1, '000001 11 1000 11 11 1010': 16, '000010 11 0110 11 11 1010': 1, '000001 11 1000 10 11 1010': 1, '000010 11 1000 01 11 1010': 7, '000000 00 0000 00 00 0010': 471, '000010 11 0100 01 11 1010': 2, '000001 01 1100 11 11 1010': 1, '000000 00 1000 10 10 1010': 4, '000010 11 1000 01 11 1101': 1, '000011 11 0100 11 11 1010': 1, '000001 11 1010 11 11 1010': 1, '000000 00 0000 00 11 1010': 160, '000011 11 1000 11 11 1010': 1, '000000 00 0000 00 00 0011': 21, '000010 11 1000 11 11 1111': 1, '000000 00 0000 00 10 1010': 63, '000000 00 0000 00 11 1101': 1, '000001 10 1000 11 11 1010': 1, '000000 00 1000 01 11 1010': 3, '000010 11 0100 11 11 1011': 1, '000000 00 0010 10 11 1010': 1, '000000 00 1000 11 11 1101': 1, '000001 11 1000 11 10 1010': 8, '000010 11 0100 11 11 1010': 15, '000000 00 1000 11 11 1010': 22, '000001 11 1100 11 11 1010': 2, '000000 00 1000 10 11 1010': 4, '000001 11 0100 01 11 1010': 2, '000000 00 0000 00 01 1011': 1, '000000 00 0100 11 10 1010': 3, '000010 10 1000 11 11 1010': 2, '000010 01 1000 11 01 1010': 1, '000010 01 1000 01 11 1010': 1, '000010 11 1000 11 11 1010': 43, '001010 11 1000 11 11 1010': 1, '000011 11 1000 11 10 1010': 1, '000000 00 1000 11 10 1010': 4, '000000 00 0100 10 11 1010': 3, '000010 01 0110 11 11 1010': 1, '000010 11 1000 11 10 1010': 11, '000010 10 1000 11 10 1011': 1, '000010 11 1000 11 01 1010': 5, '000000 00 1000 01 01 1111': 1, '000000 00 0000 00 11 1011': 8, '000000 00 0110 11 10 1010': 1, '000010 11 1000 10 01 1010': 1, '000010 11 0100 11 01 1010': 4, '000000 00 0010 11 11 1010': 2, '000010 11 0100 10 10 1010': 1, '000000 00 0100 11 11 1010': 6, '000000 00 0000 00 10 1011': 2, '000000 00 0100 01 10 1010': 1, '000000 00 0000 00 01 1010': 18, '000001 11 1010 11 11 1011': 1, '000010 11 0100 10 01 1010': 1, '000010 11 1010 11 11 1010': 3, '000010 10 0100 01 01 1010': 1, '000010 11 1000 11 11 1011': 2, '000001 11 1010 01 10 1010': 1, '000000 00 1010 11 11 1010': 1, '000001 11 0010 11 11 1010': 1, '000001 01 0100 11 11 1010': 1, '100000 11 0100 11 11 1010': 1, '000010 11 1000 10 11 1010': 7, '000010 11 1000 11 11 1101': 1, '000000 00 0110 11 11 1010': 1, '000010 01 1000 11 11 1010': 4, '000000 00 0000 00 10 1111': 2, '000000 00 1000 01 10 1010': 1, '001001 11 0110 11 01 1101': 1, '000001 11 1010 11 10 1010': 1, '000010 11 1010 11 10 1010': 3, '000000 00 1000 11 01 1010': 2}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: &quot;Courier New&quot;,Courier,monospace\">       ┌─────────────┐                                       ┌─┐              »\n",
       "q21_0: ┤ Ry(0.42212) ├──■───────────────────────■────────────┤M├──────────────»\n",
       "       └────┬───┬────┘┌─┴─┐               ┌─────┴──────┐     └╥┘              »\n",
       "q21_1: ─────┤ X ├─────┤ X ├───────■───────┤ Ry(2.6623) ├──■───╫───────────────»\n",
       "        ┌───┴───┴────┐└───┘┌──────┴──────┐└────────────┘┌─┴─┐ ║               »\n",
       "q21_2: ─┤ Ry(2.3995) ├─────┤ Ry(0.80132) ├──────■───────┤ X ├─╫───────■───────»\n",
       "        ├────────────┤     └─────────────┘    ┌─┴─┐     └───┘ ║ ┌─────┴──────┐»\n",
       "q21_3: ─┤ Ry(1.5565) ├────────────────────────┤ X ├───────────╫─┤ Ry(1.4121) ├»\n",
       "        └────────────┘                        └───┘           ║ └────────────┘»\n",
       "q21_4: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_5: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_6: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_7: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_8: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_9: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "c2: 20/═══════════════════════════════════════════════════════╩═══════════════»\n",
       "                                                              0               »\n",
       "«                                                                            »\n",
       "«q21_0: ─────────────────────────────────────────────────────────────────────»\n",
       "«       ┌─┐                                                                  »\n",
       "«q21_1: ┤M├──────────────────────────────────────────────────────────────────»\n",
       "«       └╥┘┌─┐                                                               »\n",
       "«q21_2: ─╫─┤M├───────────────────────────────────────────────────────────────»\n",
       "«        ║ └╥┘┌─┐                                                            »\n",
       "«q21_3: ─╫──╫─┤M├────────────────────────────────────────────────────────────»\n",
       "«        ║  ║ └╥┘┌────── ┌──────── ┌───────────┐                    ───────┐ »\n",
       "«q21_4: ─╫──╫──╫─┤       ┤         ┤ Ry(2.047) ├──■────────■───────        ├─»\n",
       "«        ║  ║  ║ │ If-0  │ Else-0  └───┬───┬───┘┌─┴─┐┌─────┴──────┐  End-0 │ »\n",
       "«q21_5: ─╫──╫──╫─┤       ┤         ────┤ X ├────┤ X ├┤ Ry(2.4778) ├        ├─»\n",
       "«        ║  ║  ║ └──╥─── └────────     └───┘    └───┘└────────────┘ ───────┘ »\n",
       "«q21_6: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_7: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_8: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_9: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║ ┌──╨──┐                                                     »\n",
       "«c2: 20/═╩══╩══╩═╡ 0x0 ╞═════════════════════════════════════════════════════»\n",
       "«        1  2  3 └─────┘                                                     »\n",
       "«                                                                   »\n",
       "«q21_0: ────────────────────────────────────────────────────────────»\n",
       "«                                                                   »\n",
       "«q21_1: ────────────────────────────────────────────────────────────»\n",
       "«               ┌──────   ┌───┐ ───────┐                            »\n",
       "«q21_2: ────────┤ If-0  ──┤ X ├  End-0 ├────────────────────────────»\n",
       "«               └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ »\n",
       "«q21_3: ───────────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─»\n",
       "«       ┌─┐        ║                       └──╥───   └───┘ ───────┘ »\n",
       "«q21_4: ┤M├────────╫──────────────────────────╫─────────────────────»\n",
       "«       └╥┘┌─┐     ║                          ║                     »\n",
       "«q21_5: ─╫─┤M├─────╫──────────────────────────╫─────────────────────»\n",
       "«        ║ └╥┘     ║                          ║                     »\n",
       "«q21_6: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_7: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_8: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_9: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║ ┌────╨─────┐               ┌────╨─────┐               »\n",
       "«c2: 20/═╩══╩═╡ c2_2=0x1 ╞═══════════════╡ c2_3=0x1 ╞═══════════════»\n",
       "«        4  5 └──────────┘               └──────────┘               »\n",
       "«                                                                     »\n",
       "«q21_0: ──────────────────────────────────────────────────────────────»\n",
       "«                                                                     »\n",
       "«q21_1: ──────────────────────────────────────────────────────────────»\n",
       "«                                                             ┌────── »\n",
       "«q21_2: ──────────────────────────────────────────────────────┤       »\n",
       "«                                                             │       »\n",
       "«q21_3: ──────────────────────────────────────────────────────┤       »\n",
       "«         ┌──────   ┌───┐ ───────┐                            │ If-0  »\n",
       "«q21_4: ──┤ If-0  ──┤ X ├  End-0 ├────────────────────────────┤       »\n",
       "«         └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ │       »\n",
       "«q21_5: ─────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─┤       »\n",
       "«            ║                       └──╥───   └───┘ ───────┘ └──╥─── »\n",
       "«q21_6: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_7: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_8: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_9: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«       ┌────╨─────┐               ┌────╨─────┐               ┌──╨──┐ »\n",
       "«c2: 20/╡ c2_4=0x1 ╞═══════════════╡ c2_5=0x1 ╞═══════════════╡ 0x0 ╞═»\n",
       "«       └──────────┘               └──────────┘               └─────┘ »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«       ┌────────  ┌────────────┐      ───────┐ ┌─┐                     »\n",
       "«q21_2: ┤         ─┤ Ry(1.5576) ├──■──        ├─┤M├─────────────────────»\n",
       "«       │         ┌┴────────────┤┌─┴─┐        │ └╥┘┌─┐                  »\n",
       "«q21_3: ┤         ┤ Ry(0.73226) ├┤ X ├        ├──╫─┤M├──────────────────»\n",
       "«       │ Else-0  └─────────────┘└───┘  End-0 │  ║ └╥┘┌────── ┌──────── »\n",
       "«q21_4: ┤         ───────■────────────        ├──╫──╫─┤       ┤         »\n",
       "«       │         ┌──────┴──────┐             │  ║  ║ │       │         »\n",
       "«q21_5: ┤         ┤ Ry(0.72529) ├─────        ├──╫──╫─┤       ┤         »\n",
       "«       └──────── └─────────────┘      ───────┘  ║  ║ │ If-0  │ Else-0  »\n",
       "«q21_6: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_7: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ └──╥─── └──────── »\n",
       "«q21_8: ─────────────────────────────────────────╫──╫────╫──────────────»\n",
       "«                                                ║  ║    ║              »\n",
       "«q21_9: ─────────────────────────────────────────╫──╫────╫──────────────»\n",
       "«                                                ║  ║ ┌──╨──┐           »\n",
       "«c2: 20/═════════════════════════════════════════╩══╩═╡ 0x0 ╞═══════════»\n",
       "«                                                6  7 └─────┘           »\n",
       "«                                                                            »\n",
       "«q21_0: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_1: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_2: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_3: ─────────────────────────────────────────────────────────────────────»\n",
       "«                      ┌─────────────┐              ┌─┐                      »\n",
       "«q21_4: ───────────────┤ Ry(0.45519) ├──────■───────┤M├──────────────────────»\n",
       "«       ┌─────────────┐└──────┬──────┘┌─────┴──────┐└╥┘┌─┐                   »\n",
       "«q21_5: ┤ Ry(0.72491) ├───────■───────┤ Ry(1.6046) ├─╫─┤M├───────────────────»\n",
       "«       └─────────────┘ ┌────────────┐└────────────┘ ║ └╥┘┌─┐                »\n",
       "«q21_6: ────────────────┤ Ry(1.3157) ├──────■────────╫──╫─┤M├────────────────»\n",
       "«        ┌────────────┐ └─────┬──────┘┌─────┴──────┐ ║  ║ └╥┘┌─┐┌────── ┌───┐»\n",
       "«q21_7: ─┤ Ry(2.4167) ├───────■───────┤ Ry(2.4449) ├─╫──╫──╫─┤M├┤ If-1  ┤ X ├»\n",
       "«        └────────────┘               └────────────┘ ║  ║  ║ └╥┘└──╥─── └───┘»\n",
       "«q21_8: ─────────────────────────────────────────────╫──╫──╫──╫────╫─────────»\n",
       "«                                                    ║  ║  ║  ║    ║         »\n",
       "«q21_9: ─────────────────────────────────────────────╫──╫──╫──╫────╫─────────»\n",
       "«                                                    ║  ║  ║  ║ ┌──╨──┐      »\n",
       "«c2: 20/═════════════════════════════════════════════╩══╩══╩══╩═╡ 0x0 ╞══════»\n",
       "«                                                    8  9  10 11└─────┘      »\n",
       "«                                                                          »\n",
       "«q21_0: ───────────────────────────────────────────────────────────────────»\n",
       "«                                                                          »\n",
       "«q21_1: ───────────────────────────────────────────────────────────────────»\n",
       "«                                ┌──────   ┌───┐ ───────┐                  »\n",
       "«q21_2: ─────────────────────────┤ If-0  ──┤ X ├  End-0 ├──────────────────»\n",
       "«                                └──╥───   └───┘ ───────┘   ┌──────   ┌───┐»\n",
       "«q21_3: ────────────────────────────╫───────────────────────┤ If-0  ──┤ X ├»\n",
       "«                     ───────┐      ║                       └──╥───   └───┘»\n",
       "«q21_4: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«                            │      ║                          ║           »\n",
       "«q21_5: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«                      End-0 │      ║                          ║           »\n",
       "«q21_6: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«       ┌─┐ ───────┐         │      ║                          ║           »\n",
       "«q21_7: ┤M├  End-1 ├─        ├──────╫──────────────────────────╫───────────»\n",
       "«       └╥┘ ───────┘  ───────┘      ║                          ║           »\n",
       "«q21_8: ─╫──────────────────────────╫──────────────────────────╫───────────»\n",
       "«        ║                          ║                          ║           »\n",
       "«q21_9: ─╫──────────────────────────╫──────────────────────────╫───────────»\n",
       "«        ║                     ┌────╨─────┐               ┌────╨─────┐     »\n",
       "«c2: 20/═╩═════════════════════╡ c2_6=0x1 ╞═══════════════╡ c2_7=0x1 ╞═════»\n",
       "«        11                    └──────────┘               └──────────┘     »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_2: ────────────────────────────────────────────────────────────────»\n",
       "«        ───────┐                                                       »\n",
       "«q21_3:   End-0 ├───────────────────────────────────────────────────────»\n",
       "«        ───────┘   ┌──────   ┌───┐ ───────┐                            »\n",
       "«q21_4: ────────────┤ If-0  ──┤ X ├  End-0 ├────────────────────────────»\n",
       "«                   └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ »\n",
       "«q21_5: ───────────────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─»\n",
       "«                      ║                       └──╥───   └───┘ ───────┘ »\n",
       "«q21_6: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_7: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_8: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_9: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                 ┌────╨─────┐               ┌────╨─────┐               »\n",
       "«c2: 20/══════════╡ c2_8=0x1 ╞═══════════════╡ c2_9=0x1 ╞═══════════════»\n",
       "«                 └──────────┘               └──────────┘               »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«                                                               ┌────── »\n",
       "«q21_2: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │       »\n",
       "«q21_3: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │ If-0  »\n",
       "«q21_4: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │       »\n",
       "«q21_5: ────────────────────────────────────────────────────────┤       »\n",
       "«          ┌──────   ┌───┐ ───────┐                             └──╥─── »\n",
       "«q21_6: ───┤ If-0  ──┤ X ├  End-0 ├────────────────────────────────╫────»\n",
       "«          └──╥───   └───┘ ───────┘    ┌──────   ┌───┐ ───────┐    ║    »\n",
       "«q21_7: ──────╫────────────────────────┤ If-0  ──┤ X ├  End-0 ├────╫────»\n",
       "«             ║                        └──╥───   └───┘ ───────┘    ║    »\n",
       "«q21_8: ──────╫───────────────────────────╫────────────────────────╫────»\n",
       "«             ║                           ║                        ║    »\n",
       "«q21_9: ──────╫───────────────────────────╫────────────────────────╫────»\n",
       "«       ┌─────╨─────┐               ┌─────╨─────┐               ┌──╨──┐ »\n",
       "«c2: 20/╡ c2_10=0x1 ╞═══════════════╡ c2_11=0x1 ╞═══════════════╡ 0x0 ╞═»\n",
       "«       └───────────┘               └───────────┘               └─────┘ »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«       ┌────────  ┌────────────┐      ───────┐ ┌─┐                     »\n",
       "«q21_2: ┤         ─┤ Ry(2.0178) ├──■──        ├─┤M├─────────────────────»\n",
       "«       │         ┌┴────────────┤┌─┴─┐        │ └╥┘┌─┐                  »\n",
       "«q21_3: ┤         ┤ Ry(0.58404) ├┤ X ├        ├──╫─┤M├──────────────────»\n",
       "«       │ Else-0  └─────────────┘└───┘  End-0 │  ║ └╥┘┌────── ┌──────── »\n",
       "«q21_4: ┤         ───────■────────────        ├──╫──╫─┤       ┤         »\n",
       "«       │          ┌─────┴──────┐             │  ║  ║ │       │         »\n",
       "«q21_5: ┤         ─┤ Ry(3.1182) ├─────        ├──╫──╫─┤       ┤         »\n",
       "«       └────────  └────────────┘      ───────┘  ║  ║ │       │         »\n",
       "«q21_6: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │ If-0  │ Else-0  »\n",
       "«q21_7: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_8: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_9: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ └──╥─── └──────── »\n",
       "«                                                ║  ║ ┌──╨──┐           »\n",
       "«c2: 20/═════════════════════════════════════════╩══╩═╡ 0x0 ╞═══════════»\n",
       "«                                                12 13└─────┘           »\n",
       "«                                                                             »\n",
       "«q21_0: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_1: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_2: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_3: ──────────────────────────────────────────────────────────────────────»\n",
       "«                       ┌────────────┐              ┌─┐                       »\n",
       "«q21_4: ────────────────┤ Ry(1.1333) ├──────■───────┤M├───────────────────────»\n",
       "«        ┌────────────┐ └─────┬──────┘┌─────┴──────┐└╥┘┌─┐                    »\n",
       "«q21_5: ─┤ Ry(2.8652) ├───────■───────┤ Ry(2.6879) ├─╫─┤M├────────────────────»\n",
       "«        └────────────┘ ┌───────────┐ └────────────┘ ║ └╥┘┌─┐                 »\n",
       "«q21_6: ────────────────┤ Ry(1.471) ├───────■────────╫──╫─┤M├─────────────────»\n",
       "«       ┌──────────────┐└─────┬─────┘ ┌─────┴──────┐ ║  ║ └╥┘┌─┐              »\n",
       "«q21_7: ┤ Ry(0.071173) ├──────■───────┤ Ry(2.2338) ├─╫──╫──╫─┤M├──────────────»\n",
       "«       └──────────────┘┌────────────┐└────────────┘ ║  ║  ║ └╥┘┌─┐           »\n",
       "«q21_8: ────────────────┤ Ry(1.3038) ├──────■────────╫──╫──╫──╫─┤M├───────────»\n",
       "«       ┌─────────────┐ └─────┬──────┘┌─────┴──────┐ ║  ║  ║  ║ └╥┘┌─┐┌────── »\n",
       "«q21_9: ┤ Ry(0.20523) ├───────■───────┤ Ry(2.6237) ├─╫──╫──╫──╫──╫─┤M├┤ If-1  »\n",
       "«       └─────────────┘               └────────────┘ ║  ║  ║  ║  ║ └╥┘└──╥─── »\n",
       "«                                                    ║  ║  ║  ║  ║  ║ ┌──╨──┐ »\n",
       "«c2: 20/═════════════════════════════════════════════╩══╩══╩══╩══╩══╩═╡ 0x0 ╞═»\n",
       "«                                                    14 15 16 17 18 19└─────┘ »\n",
       "«                                   \n",
       "«q21_0: ────────────────────────────\n",
       "«                                   \n",
       "«q21_1: ────────────────────────────\n",
       "«                                   \n",
       "«q21_2: ────────────────────────────\n",
       "«                                   \n",
       "«q21_3: ────────────────────────────\n",
       "«                          ───────┐ \n",
       "«q21_4: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_5: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_6: ──────────────────        ├─\n",
       "«                           End-0 │ \n",
       "«q21_7: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_8: ──────────────────        ├─\n",
       "«       ┌───┐┌─┐ ───────┐         │ \n",
       "«q21_9: ┤ X ├┤M├  End-1 ├─        ├─\n",
       "«       └───┘└╥┘ ───────┘  ───────┘ \n",
       "«c2: 20/══════╩═════════════════════\n",
       "«             19                    </pre>"
      ],
      "text/plain": [
       "       ┌─────────────┐                                       ┌─┐              »\n",
       "q21_0: ┤ Ry(0.42212) ├──■───────────────────────■────────────┤M├──────────────»\n",
       "       └────┬───┬────┘┌─┴─┐               ┌─────┴──────┐     └╥┘              »\n",
       "q21_1: ─────┤ X ├─────┤ X ├───────■───────┤ Ry(2.6623) ├──■───╫───────────────»\n",
       "        ┌───┴───┴────┐└───┘┌──────┴──────┐└────────────┘┌─┴─┐ ║               »\n",
       "q21_2: ─┤ Ry(2.3995) ├─────┤ Ry(0.80132) ├──────■───────┤ X ├─╫───────■───────»\n",
       "        ├────────────┤     └─────────────┘    ┌─┴─┐     └───┘ ║ ┌─────┴──────┐»\n",
       "q21_3: ─┤ Ry(1.5565) ├────────────────────────┤ X ├───────────╫─┤ Ry(1.4121) ├»\n",
       "        └────────────┘                        └───┘           ║ └────────────┘»\n",
       "q21_4: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_5: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_6: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_7: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_8: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "q21_9: ───────────────────────────────────────────────────────╫───────────────»\n",
       "                                                              ║               »\n",
       "c2: 20/═══════════════════════════════════════════════════════╩═══════════════»\n",
       "                                                              0               »\n",
       "«                                                                            »\n",
       "«q21_0: ─────────────────────────────────────────────────────────────────────»\n",
       "«       ┌─┐                                                                  »\n",
       "«q21_1: ┤M├──────────────────────────────────────────────────────────────────»\n",
       "«       └╥┘┌─┐                                                               »\n",
       "«q21_2: ─╫─┤M├───────────────────────────────────────────────────────────────»\n",
       "«        ║ └╥┘┌─┐                                                            »\n",
       "«q21_3: ─╫──╫─┤M├────────────────────────────────────────────────────────────»\n",
       "«        ║  ║ └╥┘┌────── ┌──────── ┌───────────┐                    ───────┐ »\n",
       "«q21_4: ─╫──╫──╫─┤       ┤         ┤ Ry(2.047) ├──■────────■───────        ├─»\n",
       "«        ║  ║  ║ │ If-0  │ Else-0  └───┬───┬───┘┌─┴─┐┌─────┴──────┐  End-0 │ »\n",
       "«q21_5: ─╫──╫──╫─┤       ┤         ────┤ X ├────┤ X ├┤ Ry(2.4778) ├        ├─»\n",
       "«        ║  ║  ║ └──╥─── └────────     └───┘    └───┘└────────────┘ ───────┘ »\n",
       "«q21_6: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_7: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_8: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║    ║                                                        »\n",
       "«q21_9: ─╫──╫──╫────╫────────────────────────────────────────────────────────»\n",
       "«        ║  ║  ║ ┌──╨──┐                                                     »\n",
       "«c2: 20/═╩══╩══╩═╡ 0x0 ╞═════════════════════════════════════════════════════»\n",
       "«        1  2  3 └─────┘                                                     »\n",
       "«                                                                   »\n",
       "«q21_0: ────────────────────────────────────────────────────────────»\n",
       "«                                                                   »\n",
       "«q21_1: ────────────────────────────────────────────────────────────»\n",
       "«               ┌──────   ┌───┐ ───────┐                            »\n",
       "«q21_2: ────────┤ If-0  ──┤ X ├  End-0 ├────────────────────────────»\n",
       "«               └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ »\n",
       "«q21_3: ───────────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─»\n",
       "«       ┌─┐        ║                       └──╥───   └───┘ ───────┘ »\n",
       "«q21_4: ┤M├────────╫──────────────────────────╫─────────────────────»\n",
       "«       └╥┘┌─┐     ║                          ║                     »\n",
       "«q21_5: ─╫─┤M├─────╫──────────────────────────╫─────────────────────»\n",
       "«        ║ └╥┘     ║                          ║                     »\n",
       "«q21_6: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_7: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_8: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║      ║                          ║                     »\n",
       "«q21_9: ─╫──╫──────╫──────────────────────────╫─────────────────────»\n",
       "«        ║  ║ ┌────╨─────┐               ┌────╨─────┐               »\n",
       "«c2: 20/═╩══╩═╡ c2_2=0x1 ╞═══════════════╡ c2_3=0x1 ╞═══════════════»\n",
       "«        4  5 └──────────┘               └──────────┘               »\n",
       "«                                                                     »\n",
       "«q21_0: ──────────────────────────────────────────────────────────────»\n",
       "«                                                                     »\n",
       "«q21_1: ──────────────────────────────────────────────────────────────»\n",
       "«                                                             ┌────── »\n",
       "«q21_2: ──────────────────────────────────────────────────────┤       »\n",
       "«                                                             │       »\n",
       "«q21_3: ──────────────────────────────────────────────────────┤       »\n",
       "«         ┌──────   ┌───┐ ───────┐                            │ If-0  »\n",
       "«q21_4: ──┤ If-0  ──┤ X ├  End-0 ├────────────────────────────┤       »\n",
       "«         └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ │       »\n",
       "«q21_5: ─────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─┤       »\n",
       "«            ║                       └──╥───   └───┘ ───────┘ └──╥─── »\n",
       "«q21_6: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_7: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_8: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«            ║                          ║                        ║    »\n",
       "«q21_9: ─────╫──────────────────────────╫────────────────────────╫────»\n",
       "«       ┌────╨─────┐               ┌────╨─────┐               ┌──╨──┐ »\n",
       "«c2: 20/╡ c2_4=0x1 ╞═══════════════╡ c2_5=0x1 ╞═══════════════╡ 0x0 ╞═»\n",
       "«       └──────────┘               └──────────┘               └─────┘ »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«       ┌────────  ┌────────────┐      ───────┐ ┌─┐                     »\n",
       "«q21_2: ┤         ─┤ Ry(1.5576) ├──■──        ├─┤M├─────────────────────»\n",
       "«       │         ┌┴────────────┤┌─┴─┐        │ └╥┘┌─┐                  »\n",
       "«q21_3: ┤         ┤ Ry(0.73226) ├┤ X ├        ├──╫─┤M├──────────────────»\n",
       "«       │ Else-0  └─────────────┘└───┘  End-0 │  ║ └╥┘┌────── ┌──────── »\n",
       "«q21_4: ┤         ───────■────────────        ├──╫──╫─┤       ┤         »\n",
       "«       │         ┌──────┴──────┐             │  ║  ║ │       │         »\n",
       "«q21_5: ┤         ┤ Ry(0.72529) ├─────        ├──╫──╫─┤       ┤         »\n",
       "«       └──────── └─────────────┘      ───────┘  ║  ║ │ If-0  │ Else-0  »\n",
       "«q21_6: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_7: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ └──╥─── └──────── »\n",
       "«q21_8: ─────────────────────────────────────────╫──╫────╫──────────────»\n",
       "«                                                ║  ║    ║              »\n",
       "«q21_9: ─────────────────────────────────────────╫──╫────╫──────────────»\n",
       "«                                                ║  ║ ┌──╨──┐           »\n",
       "«c2: 20/═════════════════════════════════════════╩══╩═╡ 0x0 ╞═══════════»\n",
       "«                                                6  7 └─────┘           »\n",
       "«                                                                            »\n",
       "«q21_0: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_1: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_2: ─────────────────────────────────────────────────────────────────────»\n",
       "«                                                                            »\n",
       "«q21_3: ─────────────────────────────────────────────────────────────────────»\n",
       "«                      ┌─────────────┐              ┌─┐                      »\n",
       "«q21_4: ───────────────┤ Ry(0.45519) ├──────■───────┤M├──────────────────────»\n",
       "«       ┌─────────────┐└──────┬──────┘┌─────┴──────┐└╥┘┌─┐                   »\n",
       "«q21_5: ┤ Ry(0.72491) ├───────■───────┤ Ry(1.6046) ├─╫─┤M├───────────────────»\n",
       "«       └─────────────┘ ┌────────────┐└────────────┘ ║ └╥┘┌─┐                »\n",
       "«q21_6: ────────────────┤ Ry(1.3157) ├──────■────────╫──╫─┤M├────────────────»\n",
       "«        ┌────────────┐ └─────┬──────┘┌─────┴──────┐ ║  ║ └╥┘┌─┐┌────── ┌───┐»\n",
       "«q21_7: ─┤ Ry(2.4167) ├───────■───────┤ Ry(2.4449) ├─╫──╫──╫─┤M├┤ If-1  ┤ X ├»\n",
       "«        └────────────┘               └────────────┘ ║  ║  ║ └╥┘└──╥─── └───┘»\n",
       "«q21_8: ─────────────────────────────────────────────╫──╫──╫──╫────╫─────────»\n",
       "«                                                    ║  ║  ║  ║    ║         »\n",
       "«q21_9: ─────────────────────────────────────────────╫──╫──╫──╫────╫─────────»\n",
       "«                                                    ║  ║  ║  ║ ┌──╨──┐      »\n",
       "«c2: 20/═════════════════════════════════════════════╩══╩══╩══╩═╡ 0x0 ╞══════»\n",
       "«                                                    8  9  10 11└─────┘      »\n",
       "«                                                                          »\n",
       "«q21_0: ───────────────────────────────────────────────────────────────────»\n",
       "«                                                                          »\n",
       "«q21_1: ───────────────────────────────────────────────────────────────────»\n",
       "«                                ┌──────   ┌───┐ ───────┐                  »\n",
       "«q21_2: ─────────────────────────┤ If-0  ──┤ X ├  End-0 ├──────────────────»\n",
       "«                                └──╥───   └───┘ ───────┘   ┌──────   ┌───┐»\n",
       "«q21_3: ────────────────────────────╫───────────────────────┤ If-0  ──┤ X ├»\n",
       "«                     ───────┐      ║                       └──╥───   └───┘»\n",
       "«q21_4: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«                            │      ║                          ║           »\n",
       "«q21_5: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«                      End-0 │      ║                          ║           »\n",
       "«q21_6: ─────────────        ├──────╫──────────────────────────╫───────────»\n",
       "«       ┌─┐ ───────┐         │      ║                          ║           »\n",
       "«q21_7: ┤M├  End-1 ├─        ├──────╫──────────────────────────╫───────────»\n",
       "«       └╥┘ ───────┘  ───────┘      ║                          ║           »\n",
       "«q21_8: ─╫──────────────────────────╫──────────────────────────╫───────────»\n",
       "«        ║                          ║                          ║           »\n",
       "«q21_9: ─╫──────────────────────────╫──────────────────────────╫───────────»\n",
       "«        ║                     ┌────╨─────┐               ┌────╨─────┐     »\n",
       "«c2: 20/═╩═════════════════════╡ c2_6=0x1 ╞═══════════════╡ c2_7=0x1 ╞═════»\n",
       "«        11                    └──────────┘               └──────────┘     »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_2: ────────────────────────────────────────────────────────────────»\n",
       "«        ───────┐                                                       »\n",
       "«q21_3:   End-0 ├───────────────────────────────────────────────────────»\n",
       "«        ───────┘   ┌──────   ┌───┐ ───────┐                            »\n",
       "«q21_4: ────────────┤ If-0  ──┤ X ├  End-0 ├────────────────────────────»\n",
       "«                   └──╥───   └───┘ ───────┘   ┌──────   ┌───┐ ───────┐ »\n",
       "«q21_5: ───────────────╫───────────────────────┤ If-0  ──┤ X ├  End-0 ├─»\n",
       "«                      ║                       └──╥───   └───┘ ───────┘ »\n",
       "«q21_6: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_7: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_8: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                      ║                          ║                     »\n",
       "«q21_9: ───────────────╫──────────────────────────╫─────────────────────»\n",
       "«                 ┌────╨─────┐               ┌────╨─────┐               »\n",
       "«c2: 20/══════════╡ c2_8=0x1 ╞═══════════════╡ c2_9=0x1 ╞═══════════════»\n",
       "«                 └──────────┘               └──────────┘               »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«                                                               ┌────── »\n",
       "«q21_2: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │       »\n",
       "«q21_3: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │ If-0  »\n",
       "«q21_4: ────────────────────────────────────────────────────────┤       »\n",
       "«                                                               │       »\n",
       "«q21_5: ────────────────────────────────────────────────────────┤       »\n",
       "«          ┌──────   ┌───┐ ───────┐                             └──╥─── »\n",
       "«q21_6: ───┤ If-0  ──┤ X ├  End-0 ├────────────────────────────────╫────»\n",
       "«          └──╥───   └───┘ ───────┘    ┌──────   ┌───┐ ───────┐    ║    »\n",
       "«q21_7: ──────╫────────────────────────┤ If-0  ──┤ X ├  End-0 ├────╫────»\n",
       "«             ║                        └──╥───   └───┘ ───────┘    ║    »\n",
       "«q21_8: ──────╫───────────────────────────╫────────────────────────╫────»\n",
       "«             ║                           ║                        ║    »\n",
       "«q21_9: ──────╫───────────────────────────╫────────────────────────╫────»\n",
       "«       ┌─────╨─────┐               ┌─────╨─────┐               ┌──╨──┐ »\n",
       "«c2: 20/╡ c2_10=0x1 ╞═══════════════╡ c2_11=0x1 ╞═══════════════╡ 0x0 ╞═»\n",
       "«       └───────────┘               └───────────┘               └─────┘ »\n",
       "«                                                                       »\n",
       "«q21_0: ────────────────────────────────────────────────────────────────»\n",
       "«                                                                       »\n",
       "«q21_1: ────────────────────────────────────────────────────────────────»\n",
       "«       ┌────────  ┌────────────┐      ───────┐ ┌─┐                     »\n",
       "«q21_2: ┤         ─┤ Ry(2.0178) ├──■──        ├─┤M├─────────────────────»\n",
       "«       │         ┌┴────────────┤┌─┴─┐        │ └╥┘┌─┐                  »\n",
       "«q21_3: ┤         ┤ Ry(0.58404) ├┤ X ├        ├──╫─┤M├──────────────────»\n",
       "«       │ Else-0  └─────────────┘└───┘  End-0 │  ║ └╥┘┌────── ┌──────── »\n",
       "«q21_4: ┤         ───────■────────────        ├──╫──╫─┤       ┤         »\n",
       "«       │          ┌─────┴──────┐             │  ║  ║ │       │         »\n",
       "«q21_5: ┤         ─┤ Ry(3.1182) ├─────        ├──╫──╫─┤       ┤         »\n",
       "«       └────────  └────────────┘      ───────┘  ║  ║ │       │         »\n",
       "«q21_6: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │ If-0  │ Else-0  »\n",
       "«q21_7: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_8: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ │       │         »\n",
       "«q21_9: ─────────────────────────────────────────╫──╫─┤       ┤         »\n",
       "«                                                ║  ║ └──╥─── └──────── »\n",
       "«                                                ║  ║ ┌──╨──┐           »\n",
       "«c2: 20/═════════════════════════════════════════╩══╩═╡ 0x0 ╞═══════════»\n",
       "«                                                12 13└─────┘           »\n",
       "«                                                                             »\n",
       "«q21_0: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_1: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_2: ──────────────────────────────────────────────────────────────────────»\n",
       "«                                                                             »\n",
       "«q21_3: ──────────────────────────────────────────────────────────────────────»\n",
       "«                       ┌────────────┐              ┌─┐                       »\n",
       "«q21_4: ────────────────┤ Ry(1.1333) ├──────■───────┤M├───────────────────────»\n",
       "«        ┌────────────┐ └─────┬──────┘┌─────┴──────┐└╥┘┌─┐                    »\n",
       "«q21_5: ─┤ Ry(2.8652) ├───────■───────┤ Ry(2.6879) ├─╫─┤M├────────────────────»\n",
       "«        └────────────┘ ┌───────────┐ └────────────┘ ║ └╥┘┌─┐                 »\n",
       "«q21_6: ────────────────┤ Ry(1.471) ├───────■────────╫──╫─┤M├─────────────────»\n",
       "«       ┌──────────────┐└─────┬─────┘ ┌─────┴──────┐ ║  ║ └╥┘┌─┐              »\n",
       "«q21_7: ┤ Ry(0.071173) ├──────■───────┤ Ry(2.2338) ├─╫──╫──╫─┤M├──────────────»\n",
       "«       └──────────────┘┌────────────┐└────────────┘ ║  ║  ║ └╥┘┌─┐           »\n",
       "«q21_8: ────────────────┤ Ry(1.3038) ├──────■────────╫──╫──╫──╫─┤M├───────────»\n",
       "«       ┌─────────────┐ └─────┬──────┘┌─────┴──────┐ ║  ║  ║  ║ └╥┘┌─┐┌────── »\n",
       "«q21_9: ┤ Ry(0.20523) ├───────■───────┤ Ry(2.6237) ├─╫──╫──╫──╫──╫─┤M├┤ If-1  »\n",
       "«       └─────────────┘               └────────────┘ ║  ║  ║  ║  ║ └╥┘└──╥─── »\n",
       "«                                                    ║  ║  ║  ║  ║  ║ ┌──╨──┐ »\n",
       "«c2: 20/═════════════════════════════════════════════╩══╩══╩══╩══╩══╩═╡ 0x0 ╞═»\n",
       "«                                                    14 15 16 17 18 19└─────┘ »\n",
       "«                                   \n",
       "«q21_0: ────────────────────────────\n",
       "«                                   \n",
       "«q21_1: ────────────────────────────\n",
       "«                                   \n",
       "«q21_2: ────────────────────────────\n",
       "«                                   \n",
       "«q21_3: ────────────────────────────\n",
       "«                          ───────┐ \n",
       "«q21_4: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_5: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_6: ──────────────────        ├─\n",
       "«                           End-0 │ \n",
       "«q21_7: ──────────────────        ├─\n",
       "«                                 │ \n",
       "«q21_8: ──────────────────        ├─\n",
       "«       ┌───┐┌─┐ ───────┐         │ \n",
       "«q21_9: ┤ X ├┤M├  End-1 ├─        ├─\n",
       "«       └───┘└╥┘ ───────┘  ───────┘ \n",
       "«c2: 20/══════╩═════════════════════\n",
       "«             19                    "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_heavy_atom = 4\n",
    "num_sample = 2**15\n",
    "assert num_heavy_atom >= 2\n",
    "num_qubits = 4 + (num_heavy_atom-1) * 2\n",
    "num_clbits = num_heavy_atom * (num_heavy_atom + 1)\n",
    "num_weights = int(8 + (num_heavy_atom - 2)*(num_heavy_atom + 3) * 3 / 2)\n",
    "print(num_weights)\n",
    "\n",
    "data_generator = MoleculeQuantumStateGenerator(num_heavy_atom)\n",
    "qubits = QuantumRegister(num_qubits)\n",
    "clbits = ClassicalRegister(num_clbits)\n",
    "qc = QuantumCircuit(qubits, clbits)\n",
    "random.seed(1)\n",
    "weight_vector = [random.random() for _ in range(num_weights)]\n",
    "\n",
    "def get_classical_register_by_name(qc: QuantumCircuit, name: str):\n",
    "    for cr in qc.cregs:\n",
    "        if cr.name == name:\n",
    "            return cr\n",
    "    else:\n",
    "        raise ValueError(f\"The {name} is not found in classical registers.\")\n",
    "\n",
    "def softmax_temperature(weight_vector, temperature=0.2):\n",
    "    weight_vector /= temperature\n",
    "    exps = np.exp(weight_vector)\n",
    "    return exps / np.sum(exps)\n",
    "\n",
    "def controlled_ry(control:int, target:int, digit:float):\n",
    "    qc.cry(np.pi*digit, control, target)\n",
    "\n",
    "def reset_qubit(qubit, cbit):\n",
    "    with qc.if_test((clbits[cbit], 1)):\n",
    "        qc.x(qubit)\n",
    "\n",
    "def build_two_atom_circuit(qc, weight_vector, qubits, clbits):\n",
    "    qc.ry(np.pi * weight_vector[0], 0)\n",
    "    qc.x(1)\n",
    "    qc.ry(np.pi * weight_vector[2], 2)\n",
    "    qc.ry(np.pi * weight_vector[4], 3)\n",
    "    qc.cx(0, 1)\n",
    "    controlled_ry(1, 2, weight_vector[3])\n",
    "    qc.cx(2, 3)\n",
    "    controlled_ry(0, 1, weight_vector[1])\n",
    "    qc.cx(1, 2)\n",
    "    controlled_ry(2, 3, weight_vector[5])\n",
    "\n",
    "    # measure atom 1 state:\n",
    "    qc.measure(qubits[0:2], clbits[0:2])\n",
    "    # measure atom 2 state and save:\n",
    "    qc.measure(qubits[2:4], clbits[2:4])\n",
    "\n",
    "    # Add a new atom existence ClassicalRegister:\n",
    "    atom_existence_CR = ClassicalRegister(bits=clbits[2:4], name=\"atom_2_existence\")\n",
    "    qc.add_register(atom_existence_CR)\n",
    "    with qc.if_test((atom_existence_CR, 0)) as else_:\n",
    "        pass\n",
    "    with else_:\n",
    "        qc.ry(np.pi * weight_vector[6], 4)\n",
    "        qc.x(5)\n",
    "        qc.cx(4,5)\n",
    "        controlled_ry(4, 5, weight_vector[7])\n",
    "\n",
    "    qc.measure(qubits[4:6], clbits[4:6])\n",
    "\n",
    "def reset_previous_atom_bond_circuit(qc, heavy_idx):\n",
    "    reset_qubits_index = list(range(2, 2*heavy_idx))\n",
    "    start_clbit = (heavy_idx - 2)**2 + (heavy_idx - 2)\n",
    "    reset_clbits_index = list(range(start_clbit, start_clbit+(heavy_idx - 1)*2))\n",
    "    for qubit_index, clbit_index in zip(reset_qubits_index, reset_clbits_index):\n",
    "        with qc.if_test((clbits[clbit_index], 1)):\n",
    "            qc.x(qubit_index)\n",
    "\n",
    "def build_atom_type_circuit(qc, heavy_idx: int, weight_vector: Union[List[float], np.ndarray]):\n",
    "    assert len(weight_vector) == 3\n",
    "    qubit_1_index = 2\n",
    "    qubit_2_index = 3\n",
    "    clbit_1_index = (heavy_idx - 1)**2 + (heavy_idx - 1)\n",
    "    clbit_2_index = clbit_1_index + 1\n",
    "\n",
    "    register_name = f\"atom_{heavy_idx-1}_existence\"\n",
    "    atom_existence_CR = get_classical_register_by_name(qc, register_name)\n",
    "    with qc.if_test((atom_existence_CR, 0)) as else_:\n",
    "        pass\n",
    "    with else_:\n",
    "        qc.ry(np.pi * weight_vector[0], qubit_1_index)\n",
    "        qc.ry(np.pi * weight_vector[1], qubit_2_index)\n",
    "        qc.cx(qubit_1_index, qubit_2_index)\n",
    "        controlled_ry(4, 5, weight_vector[2])\n",
    "    qc.measure(qubits[[qubit_1_index,qubit_2_index]], clbits[[clbit_1_index,clbit_2_index]])\n",
    "    # create new atom existence register\n",
    "    atom_existence_CR = ClassicalRegister(bits=clbits[[clbit_1_index,clbit_2_index]], name=f\"atom_{heavy_idx}_existence\")\n",
    "    qc.add_register(atom_existence_CR)\n",
    "\n",
    "def build_bond_type_circuit(qc, heavy_idx: int, fixed_weight_vector: Union[List[float], np.ndarray],\n",
    "                                flexible_weight_vector: Union[List[float], np.ndarray], remove_bond_disconnection=True):\n",
    "    assert len(fixed_weight_vector) == heavy_idx-1\n",
    "    assert len(flexible_weight_vector) == 2*(heavy_idx-1)\n",
    "    qubit_start_index = 4\n",
    "    qubit_end_index = qubit_start_index + 2*(heavy_idx - 1)\n",
    "    clbit_start_index = (heavy_idx)**2 - heavy_idx + 2\n",
    "    clbit_end_index = clbit_start_index + 2*(heavy_idx - 1)\n",
    "\n",
    "    register_name = f\"atom_{heavy_idx}_existence\"\n",
    "    atom_existence_CR = get_classical_register_by_name(qc, register_name)\n",
    "    with qc.if_test((atom_existence_CR, 0)) as else_:\n",
    "        pass\n",
    "    with else_:\n",
    "        for i in range(heavy_idx-1):\n",
    "            qc.ry(np.pi * fixed_weight_vector[i], qubit_start_index+2*i+1)\n",
    "            controlled_ry(qubit_start_index+2*i+1, qubit_start_index+2*i, flexible_weight_vector[2*i]) # < 0.5\n",
    "            controlled_ry(qubit_start_index+2*i, qubit_start_index+2*i+1, flexible_weight_vector[2*i+1]) # > 0.5\n",
    "        qc.measure(qubits[qubit_start_index:qubit_end_index], clbits[clbit_start_index:clbit_end_index])\n",
    "        if remove_bond_disconnection:\n",
    "            bond_disconnection_CR = ClassicalRegister(bits=clbits[clbit_start_index:clbit_end_index], name=f\"bond_{heavy_idx}_connection\")\n",
    "            qc.add_register(bond_disconnection_CR)\n",
    "            with qc.if_test((bond_disconnection_CR, 0)):\n",
    "                qc.x(qubit_end_index-1)\n",
    "                qc.measure(qubits[qubit_end_index-1], clbits[clbit_end_index-1])\n",
    "    return\n",
    "\n",
    "build_two_atom_circuit(qc, weight_vector, qubits, clbits)\n",
    "for heavy_idx in range(3, num_heavy_atom+1):\n",
    "    atom_type_weights = [random.random() for _ in range(3)]\n",
    "    bond_type_fixed_weight_vector = np.array([random.random() for _ in range((heavy_idx-1))])\n",
    "    bond_type_fixed_weight_vector = softmax_temperature(bond_type_fixed_weight_vector, temperature=0.2)\n",
    "    bond_type_flexible_weight_vector = np.array([random.random()*0.5 for _ in range((heavy_idx-1)*2)])\n",
    "    bond_type_flexible_weight_vector += np.array([0, 0.5]*(heavy_idx-1))\n",
    "    print(bond_type_fixed_weight_vector)\n",
    "    print(bond_type_flexible_weight_vector)\n",
    "\n",
    "    reset_previous_atom_bond_circuit(qc, heavy_idx)\n",
    "    build_atom_type_circuit(qc, heavy_idx, atom_type_weights)\n",
    "    build_bond_type_circuit(qc, heavy_idx, bond_type_fixed_weight_vector,\n",
    "                                bond_type_flexible_weight_vector, remove_bond_disconnection=True)\n",
    "# simulator = Aer.get_backend('aer_simulator')\n",
    "# results = execute(qc, backend=simulator, shots=num_sample).result()\n",
    "# counts = results.get_counts(qc)\n",
    "# print(counts)\n",
    "\n",
    "simulator = Aer.get_backend('aer_simulator_statevector')\n",
    "circuit = transpile(qc, simulator)\n",
    "job = simulator.run(circuit)\n",
    "result = job.result()\n",
    "print(result.get_counts())\n",
    "\n",
    "# qc.draw(\"mpl\", style=\"mpl\")\n",
    "qc.draw(\"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{None: 3475, 'N=NN=N': 1, 'NN1C=N1': 1, 'C1=NNC1': 3, 'C1C2OC12': 1, 'N=CNO': 1, 'N#CN=N': 1, 'N=NO': 2, 'CC=NN': 5, 'C1NN1': 1, 'C#CCO': 3, 'CCN=N': 2, 'O=CC=O': 2, 'CC=CO': 5, 'C=C(C)N': 1, 'C=N': 22, 'NNOO': 1, 'CNCN': 3, 'OC1=CN1': 2, 'C=CCC': 3, 'CC(=N)N': 2, 'NN=NO': 1, 'C#CCC': 2, 'N=C=NN': 2, 'C=CNC': 1, 'NNCO': 2, 'CC#CO': 18, 'C#CCN': 8, 'CN=O': 1, 'NC1CC1': 5, '[N-]=[N+]=N': 2, 'CCC=O': 8, 'CCNN': 15, 'N#CCN': 32, 'OC#CO': 12, 'c1ooc#1': 1, 'CC=O': 13, 'CC1C=N1': 1, 'CC=C=N': 8, 'CC(C)N': 6, 'C1#CNC1': 1, 'C1=CN1': 8, 'CCCO': 6, 'NCN': 7, 'C#CN': 744, 'OC1C=N1': 2, 'C=CC': 32, 'C1#CC1': 10, 'N=CN=N': 3, 'N=CCO': 59, 'C1CC1': 4, 'CN': 80, 'C#CC': 97, 'CC=NC': 1, 'NCCN': 485, 'C=CC=N': 19, 'NC1=CC1': 4, 'N=O': 5, 'N=C=CN': 98, 'NCCO': 99, 'N=C=C=O': 8, 'O=CCO': 5, 'C=CCN': 39, 'O': 2, 'NO': 6, 'CC=CN': 18, 'CC(N)O': 1, 'C=CON': 2, 'NC#CO': 258, 'C=CO': 10, 'C1CNC1': 1, 'CCN': 284, 'N=C1CN1': 16, 'C=CC#N': 2, 'C1CO1': 8, 'N=NN': 1, 'c1[nH][nH]c#1': 8, 'CNN': 2, 'C#CNN': 30, 'C=C=C': 5, 'CC': 1927, 'N': 589, 'NNON': 3, 'N=CNN': 15, 'C': 15288, 'NC#CN': 1174, 'N=C=CO': 22, 'C=C=NN': 3, 'N=C1CC1': 4, 'NC=O': 2, 'C=[N+]=[N-]': 2, 'N1=NN1': 1, 'C#C': 4640, 'C#N': 164, 'CC#N': 22, 'N=CCN': 377, 'NC=CO': 43, 'NC1C=N1': 25, 'C#CNC': 2, 'OC1CC1': 2, 'NC1CN1': 47, 'NC=CN': 131, 'NC1=CN1': 16, 'CC#CN': 231, 'OC1CN1': 8, 'C=C=C=N': 7, 'CC=N': 126, 'C#CN=N': 1, 'N=NCN': 11, 'N#CC=N': 23, 'C#CO': 127, 'CC(N)N': 17, 'C=C': 522, 'C1#CN1': 79, 'NC1=C=N1': 5, 'NN1CC1': 3, 'C=C=N': 26, 'NN=NN': 3, 'C#CNO': 4, 'NNNN': 9, 'C1=CNC1': 1, 'CC(=N)O': 1, 'CCCN': 93, 'C1=CC=1': 1, 'OCCO': 6, 'C1=CN=1': 7, 'N=N': 7, 'N#N': 63, 'c1[nH]oc#1': 3, 'C1C2=NN12': 1, 'N=NNN': 6, 'C1CN1': 30, 'CCC=N': 48, 'C=C=O': 6, 'CC1=CN1': 1, 'NCN=O': 2, 'NC1C#C1': 2, 'CN1C#C1': 1, 'C=C=CN': 17, 'NC1CO1': 6, 'C1=NC1': 25, 'N=CC=N': 86, 'N=CN': 7, 'C=CN': 90, 'N=C=N': 2, 'C=CCO': 4, 'OC=CO': 1, 'N#CN': 30, 'CNNN': 1, 'CCN=O': 1, 'N=C=C=N': 19, 'NCC=O': 51, 'N=c1cc1': 4, 'C1#CN=N1': 6, 'CCON': 1, 'NN1C#C1': 3, 'NC1=CO1': 3, 'CC#CC': 5, 'C1=CC1': 12, 'N=CC=O': 15, 'N=c1cn1': 14, 'C#CON': 3, 'C=C1CN1': 4, 'O=C=CO': 1, 'NC=C=O': 12, 'N#CO': 4, 'C1=CO1': 1, 'NNNO': 2, 'CCNO': 2, 'OC1CO1': 1, 'CCC': 46, 'C1CN=N1': 2, 'N=C1CO1': 6, 'NN=CO': 2, 'NN1N=N1': 1, 'N#CNN': 2, 'NC#[N+][O-]': 2, 'CC1CO1': 1, 'CN=C=N': 1, 'c1c[nH][nH]1': 1, 'NN': 17, 'C=C=CO': 1, 'N=CO': 1, 'CN(N)N': 1, 'NC1C=C1': 1, 'N=NON': 1, 'O=NO': 1, 'C=C(N)N': 5, 'C1n2[nH]n21': 1, 'NNC=O': 1, 'ON1C#C1': 2, 'NC=NN': 7, 'N=NCO': 1, 'C#CC=N': 1, 'CCO': 44, 'CCC#N': 1, 'C=NCN': 1, 'NNN': 5, 'N#CON': 1, 'C1=CNN=1': 1, 'N#CCO': 3, 'NONO': 1, 'NCNN': 16, 'CCCC': 2, 'c1c2[nH]n1-2': 1, 'NN1CN1': 1, 'C1=NN1': 1, 'C=CC=O': 3, 'NN(N)N': 1, 'C=CNO': 1, 'C1#CO1': 8, 'CC1CN1': 3, 'O=C1CN1': 1, 'C1CNN1': 4, 'C=CNN': 4, 'C1CON1': 1, 'NNO': 1}\n",
      "0.893951416015625 0.006591796875\n"
     ]
    }
   ],
   "source": [
    "smiles_dict = {}\n",
    "num_valid_molecule = 0\n",
    "for key, value in counts.items():\n",
    "    key = \"\".join(key.split())\n",
    "    smiles = data_generator.QuantumStateToSmiles(data_generator.post_process_quantum_state(key))\n",
    "    smiles_dict[smiles] = smiles_dict.get(smiles, 0) + value\n",
    "    if smiles:\n",
    "        num_valid_molecule += value\n",
    "validity = num_valid_molecule / num_sample\n",
    "diversity = (len(smiles_dict.keys()) - 1) / num_sample\n",
    "print(smiles_dict)\n",
    "print(validity, diversity)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.          1.          0.          0.        ]\n",
      " [ 1.          0.          0.          0.        ]\n",
      " [ 0.          0.          0.70710678  0.70710678]\n",
      " [ 0.          0.          0.70710678 -0.70710678]]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: &quot;Courier New&quot;,Courier,monospace\">global phase: 6.1208\n",
       "       ┌───────────────┐        ┌───────────────────────┐       »\n",
       "q6_0: ─┤ U(π/2,0,-π/2) ├────■───┤ U(π,0.84253,-0.72827) ├────■──»\n",
       "      ┌┴───────────────┴─┐┌─┴─┐┌┴───────────────────────┴─┐┌─┴─┐»\n",
       "q6_1: ┤ U(0.40375,π/2,0) ├┤ X ├┤ U(1.4648,-1.2877,2.2836) ├┤ X ├»\n",
       "      └──────────────────┘└───┘└──────────────────────────┘└───┘»\n",
       "«      ┌──────────────────┐\n",
       "«q6_0: ┤ U(π/2,-π/2,3π/4) ├\n",
       "«      ├──────────────────┤\n",
       "«q6_1: ┤ U(1.9745,-π,π/2) ├\n",
       "«      └──────────────────┘</pre>"
      ],
      "text/plain": [
       "global phase: 6.1208\n",
       "       ┌───────────────┐        ┌───────────────────────┐       »\n",
       "q6_0: ─┤ U(π/2,0,-π/2) ├────■───┤ U(π,0.84253,-0.72827) ├────■──»\n",
       "      ┌┴───────────────┴─┐┌─┴─┐┌┴───────────────────────┴─┐┌─┴─┐»\n",
       "q6_1: ┤ U(0.40375,π/2,0) ├┤ X ├┤ U(1.4648,-1.2877,2.2836) ├┤ X ├»\n",
       "      └──────────────────┘└───┘└──────────────────────────┘└───┘»\n",
       "«      ┌──────────────────┐\n",
       "«q6_0: ┤ U(π/2,-π/2,3π/4) ├\n",
       "«      ├──────────────────┤\n",
       "«q6_1: ┤ U(1.9745,-π,π/2) ├\n",
       "«      └──────────────────┘"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "one_one = np.array([[1, 0], [0, 0]])\n",
    "zero_zero = np.array([[0, 0], [0, 1]])\n",
    "X = np.array([[0, 1], [1, 0]])\n",
    "H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)\n",
    "\n",
    "A = np.kron(one_one, X)\n",
    "B = np.kron(zero_zero, H)\n",
    "C = A+B\n",
    "print(C)\n",
    "# print(C.conjugate().transpose())\n",
    "# print(np.matmul(C, C.conjugate().transpose()))\n",
    "from qiskit import QuantumCircuit\n",
    "from qiskit.quantum_info import Operator\n",
    "from qiskit.quantum_info.synthesis import two_qubit_cnot_decompose\n",
    "from qiskit.quantum_info import random_unitary\n",
    "\n",
    "# 創建一個隨機的 4x4 單位矩陣\n",
    "unitary = C\n",
    "\n",
    "# 使用 two_qubit_cnot_decompose 將 4x4 矩陣分解為兩個 qubits 的量子門\n",
    "decomposed_circuit = two_qubit_cnot_decompose(Operator(unitary))\n",
    "\n",
    "# 打印分解後的量子電路\n",
    "# print(decomposed_circuit)\n",
    "decomposed_circuit.draw(style=\"mpl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'qiskit'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QuantumCircuit, transpile\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit_aer\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Aer\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcreate_ghz_circuit\u001b[39m(n_qubits):\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'qiskit'"
     ]
    }
   ],
   "source": [
    "from qiskit import QuantumCircuit, transpile\n",
    "from qiskit_aer import Aer\n",
    "\n",
    "def create_ghz_circuit(n_qubits):\n",
    "    circuit = QuantumCircuit(n_qubits)\n",
    "    circuit.h(0)\n",
    "    for qubit in range(n_qubits - 1):\n",
    "        circuit.cx(qubit, qubit + 1)\n",
    "    return circuit\n",
    "\n",
    "simulator = Aer.get_backend('aer_simulator_statevector')\n",
    "circuit = create_ghz_circuit(n_qubits=20)\n",
    "circuit.measure_all()\n",
    "circuit = transpile(circuit, simulator)\n",
    "job = simulator.run(circuit)\n",
    "result = job.result()\n",
    "\n",
    "print(result.get_counts())\n",
    "print(f'backend: {result.backend_name}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qmg",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
